using Pyxis.FluentSelenium.Locator;
using Selenium;

namespace Pyxis.FluentSelenium.Tester
{
    public class ImageTester
    {
        private readonly ImageLocator locator;
        private readonly ContentTester rootTester;

        private string imageSourceLocator
        {
            get { return locator + "@src"; }
        }

        private ISelenium selenium
        {
            get { return rootTester.Selenium; }
        }

        public ImageTester(ImageLocator locator, ContentTester rootTester)
        {
            this.locator = locator;
            this.rootTester = rootTester;
        }

        public void ShouldSeeImage(string expectedImageSource)
        {
            var actualImageSource = End(selenium.GetAttribute(imageSourceLocator), expectedImageSource.Length);
            Assert.ContentMatch(expectedImageSource, actualImageSource, locator);
        }

        private string End(string path, int length)
        {
            var startIndex = path.Length - length;
            return path.Substring(startIndex > 0 ? startIndex : 0);
        }
    }
}